﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Report.IBLL;
using CNKI.TPI.Web.Report.IDAO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace CNKI.TPI.Web.Report.BLL
{
    public class UserOperateReportService : BaseService, IUserOperateReportService
    {
        IUserOperateDAO userOperateDAO = null;
        public UserOperateReportService(IUserOperateDAO userOperateDAO)
        {
            this.userOperateDAO = this.generator.CreateInterfaceProxyWithTarget<IUserOperateDAO>(userOperateDAO, new IInterceptor[] { new LogInterceptor() });
        }

        public DataTable GetUserOperateStatisticsInfo(int searchType, DateTime? startDate, DateTime? endDate,string startip,string endip)
        {
            return userOperateDAO.GetUserOperateStatisticsInfo(searchType, startDate, endDate,startip,endip);
        }
        public DataTable GetSubmitInfo(string username, DateTime? startDate, DateTime? endDate)
        {
            return userOperateDAO.GetSubmitInfo(username,startDate,endDate);
        }

        public DataTable GetUserWorkInfo(string username, DateTime? startDate, DateTime? endDate)
        {
            DataTable result = new DataTable();
            result.Columns.Add("UserName");
            string[] workTypeList = { "记录通过编目" ,"记录通过审查" , "记录重新审查" , "记录全部通过审查" , "记录全部重新审查" };
            DataTable dt = new DataTable();
            dt.Columns.Add("logusername");
            dt.Columns.Add("workType");
            dt.Columns.Add("rowCount");
            DataTable data = null;
            foreach (string workType in workTypeList)
            {
                data = userOperateDAO.GetUserWorkInfo(username, workType, startDate, endDate);
                if (data != null && data.Rows.Count > 0)
                {
                    foreach (DataRow dr in data.Rows)
                    {
                        dt.ImportRow(dr);
                    }
                }
                result.Columns.Add(workType);
                result.Columns[workType].DefaultValue = "0";
            }

            IEnumerable<string> userList = dt.AsEnumerable().Select(d => d.Field<string>("logusername")).ToArray().Distinct();
            foreach (string userName in userList)
            {
                DataRow[] userInfo = dt.Select("logusername='"+ userName + "'");
                DataRow newRow = result.NewRow();
                newRow[0] = userName;
                foreach (DataRow dr in userInfo)
                {
                    newRow[dr[1].ToString()] = dr[2];
                }
                result.Rows.Add(newRow);
            }

            result.Columns[1].ColumnName = "Mark";
            result.Columns[2].ColumnName = "CheckApproved";
            result.Columns[3].ColumnName = "CheckDismissed";
            result.Columns[4].ColumnName = "CheckAllApproved";
            result.Columns[5].ColumnName = "CheckAllDismissed";

            return result;
        }
    }
}
